//logs.js
const util = require('../../utils/util.js')
const brandData = require('../../utils/getBrand.js')
Page({
  data:{
    category:'',
    categoryName:'',
    letter : ['A','B','C','D','F','G','H','J','K','L','M','N','O','Q','R','S','T','W','X','Y','Z'],
    intData : '',
    intBrand : '',
    letterId : '',
    itemHeight: '',
    tempObj : '',
    showLetter:false,
    nowLetter:'',
    nowBrand:'',
    nowLogo:'',
    time:'',
    mark:0,
    newmark:0,
    showSerie:false,
    buttonClicked: false
  },
  onLoad(option){
    var that = this;
    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight * 0.85;
    //添加要匹配的字母范围值
    //1、更加屏幕高度设置子元素的高度
    var itemH = winHeight / this.data.letter.length;
    this.setData({
      itemHeight: itemH,
      category : option.id,
      categoryName: option.category
    })
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    wx.request({
      url: util.http+"brands/",
      data: {
        category:that.data.category
      },
      header: {
        'content-type': 'application/json' 
      },
      success:function(res){
        that.int.intLetter.call(that,that.data.letter, res.data.results)
      },
      complete:function(res){
        wx.hideLoading();
        if (res == null || res.data == null){
          console.error('网络请求失败');
        }
      }
    })
  },

  int: {
    intLetter: function (letter, brand) {
      var obj = {};
      for (var i = 0; i < letter.length; i++) {
        var arr = [];
        for (var j = 0; j < brand.length; j++) {
          if (letter[i] == brand[j].first_letter) {
            var arr1 = [brand[j].name, brand[j].logo]
            arr.push(arr1)
          }
        }
        obj[letter[i]] = arr;
      }
      this.setData({
        intData: obj
      })
    },
    intBrand : function(brand){
      var obj = {};
      var brandData = {};
      for (var i=0;i<brand.length;i++){
        for (var j = brand.length-1;j>=0;j--){
          obj[brand[i].manufacturer] = brand[i].manufacturer
        }
      }
      for (var key in obj){
        var arr = [];
        for (var n=0; n<brand.length;n++){
          if (obj[key] == brand[n].manufacturer){
            var list = [brand[n].name,brand[n].id]
            arr.push(list)
          }
        }
        brandData[key] = arr;
      }
      this.setData({
        intBrand : brandData
      })
    }
  },

  startTouch(e) {
    var _id = e.target.dataset.id;
    clearTimeout(this.data.time)
    this.setData({
      letterId : _id,
      nowLetter : _id,
      showLetter:true
    })
  },
  endTouch(e){
    var that = this;
    this.setData({
      time : setTimeout(function(){
        that.setData({
          showLetter: false
        })
      },1500)
    })
  },
  clickBrand(e){
    var target = e.target.dataset.brand,
        logo = e.target.dataset.logo,
        that = this;
    if (this.data.showSerie){
      this.setData({
        showSerie: false
      })
    }else{
      this.setData({
        showSerie: true,
        nowBrand: target,
        nowLogo : logo
      })
    }
    wx.request({
      url:util.http+"series",
      data : {
        brand__name: that.data.nowBrand,
        category: that.data.category
      },
      header: {
        'content-type': 'application/json' 
      },
      success: function (res) {
        that.int.intBrand.call(that, res.data.results)
      },
      complete:function(res){
        if (res == null || res.data == null) {
          console.error('网络请求失败');
        }
      }
    })
  },
  clickSerie(e){
    if (!this.buttonClicked){
      util.buttonClicked(this);
      var serie = e.target.dataset.serie,
          id = e.target.dataset.id,
          category = this.data.category ,
          link = '../year/year?serie=' + serie + '&id=' + id + '&category=' + category;
      wx.navigateTo({
        url: link
      })
    } 
  },
  tap_start(e){
    var __mark = e.touches[0].pageX;
    this.setData({
      mark: __mark,
      newmark: __mark
    })
  },
  tap_move(e){
    this.data.newmark = e.touches[0].pageX;
    if (this.data.newmark - this.data.mark > wx.getSystemInfoSync().windowHeight * 0.15  ) {
      this.istoright = true;
    }
    if (this.data.newmark - this.data.mark < wx.getSystemInfoSync().windowHeight * 0.15 ) {
      this.istoright = false;
    }
    
  },
  tap_end(e){
    if (this.data.mark != this.data.newmark){
      if (this.istoright) {
        this.setData({
          showSerie: false
        });
      } else {
        this.setData({
          showSerie: true
        });
      }
      this.data.mark = 0;
      this.data.newmark = 0;
    }
  }
})
